import { getRotation, isArray } from '../utils/index'
import { Icon, Style, Stroke, Fill, Text } from 'ol/style'
import imgMarker from '../static/image/2004.png'

export {
  getStyle
}
/**
 * 常规样式
 * @param {*} feature
 */
function normal (feature) {
  // 获取属性
  const {
    fillColor = "rgba( 255, 255, 255, 0.7)",
    strokeColor = '#f4b49f',
    strokeWidth = 1
  } = feature.get('property') || {}

  return new Style({
    stroke: new Stroke({
      color: strokeColor || fillColor,
      width: strokeWidth
    }),
    fill: new Fill({
      color: fillColor
    }),
    // text: textStyle(feature, true)
  })
}


/**
 * 添加文字
 * @param {*} feature
 * @param {Object} config 其他的配置
 */
function textStyle (feature, config) {
  const { textShow = true } = config || {}
  const property = feature.get('property') || {}
  // 优先取传入的name, 如果没有值则取property内的
  const { text = {}, textColor, color, name: title } = property
  const name = config.name || feature.get('name') || title
  if (!textShow || !name) return ''
  // 暴露出去的文字属性配置
  const {
    fill,
    strokeWidth = 1,
    strokeColor,
    textAlign = 'center',
    textSize = '12px',
    font = 'Microsoft YaHei Regular',
    textBaseline = 'top',
    offsetX = 0,
    offsetY = 0
  } = text
  const fillColor = fill || textColor || color || '#333333'
  const textConfig = {
    fill: new Fill({ color: fillColor }),
    textAlign,
    textBaseline,
    font: `bold ${textSize} ${font}`,
    offsetX,
    offsetY,
    text: name,
    ...config
  }
  if (strokeColor) {
    textConfig.stroke = new Stroke({
      color: strokeColor,
      width: strokeWidth
    })
  }
  return new Text(textConfig)
}

/**
 * 线段
 */
function lineString (feature) {
  const {
    color = '#333333',
    lineWidth = 1.5,
    lineDash = 6, // 虚线间隔距离
    icon = [],
    data = [],
    showDistance,
    distance,
    name
  } = feature.get('property') || {}

  const lineType = feature.get('lineType')
  const fillColor = 'rgba(0, 0, 255, 0.1)'
  const rotation = getRotation(data)
  const strokeOpts = {
    color,
    width: lineWidth
  }
  if (lineType === 'dash') {
    strokeOpts.lineDash = [lineDash]
  }
  const textShow = this.getDistanceFromPixel() <= 200

  const textOpts = {
    textShow,
    rotateWithView: true,
    rotation: -rotation // 角度
  }
  const styles = [
    new Style({
      stroke: new Stroke(strokeOpts),
      fill: new Fill({ color: fillColor }),
      // 文字
      text: textStyle(feature, textOpts),
    })
  ]
  // 给线路添加距离
  if (showDistance) {
    const textStyle = new Style({
      text: textStyle(feature, {
        name: distance || name,
        ...textOpts
      })
    })
    styles.push(textStyle)
  }
  // 线路添加其他图标
  const iconStyle = addLineStringIcon(icon, rotation)
  styles.push(...iconStyle)

  return styles
}
/**
 * 线路添加图标
 * @param {*} icon 
 * @param {*} rotation 
 * @returns 
 */
function addLineStringIcon (icon, rotation) {
  const iconStyles = []
  if (!isArray(icon)) return
  // 给线路添加其他图标
  icon.forEach(({ coordinate, config = {} }) => {
    if (!isArray(coordinate) || !config.src) return
    iconStyles.push(new Style({
      geometry: new Point(coordinate),
      image: new Icon({
        rotateWithView: true,
        rotation: -rotation,
        ...config
      })
    }))
  })
  return iconStyles
}
/**
 * 绘制icon类型
 * @param {*} feature
 */
function icon (feature) {
  const pixel = this.getDistanceFromPixel()
  const src = feature.get('imgType') || imgMarker
  // icon的其他配置
  const { icon = {} } = feature.get('property') || {}
  const defaultScale = pixel > 500 ? 0.5 : 0.7
  const {
    anchor = [0.5, 28],
    scale = defaultScale,
    offset = [0, 0]
  } = icon

  return new Style({
    image: new Icon({
      anchor,
      anchorXUnits: 'fraction',
      anchorYUnits: 'pixels',
      scale,
      src,
      offset
    }),
    text: textStyle(feature)
  })
}


/**
 * @description 绘制纯文本
 * @param {*} feature
 * @returns
 */
function text (feature) {
  return new Style({
    text: textStyle(feature)
  })
}

const featureStyles = {
  icon,
  text,
  lineString,
  normal
}


function getStyle (feature) {
  const type = feature.get('type')
  const fn = featureStyles[type]
  if (fn) {
    return fn.call(this, feature)
  }
  return featureStyles.normal(feature)
}

